/*
 * PaletteSupport.java
 *
 * Created on September 25, 2006, 2:22 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 *
 * To understand this class, see http://platform.netbeans.org/tutorials/nbm-nodesapi3.html
 */
package org.put.netbeans.dcs_modeler.palette;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.logging.Logger;
import javax.swing.Action;
import org.netbeans.spi.palette.DragAndDropHandler;
import org.netbeans.spi.palette.PaletteActions;
import org.netbeans.spi.palette.PaletteController;
import org.netbeans.spi.palette.PaletteFactory;
import org.openide.nodes.AbstractNode;
import org.openide.util.Lookup;
import org.openide.util.datatransfer.ExTransferable;
import org.put.netbeans.dcs_modeler.wizards.importWorkflowElement.ImportWorkflowElementWizardAction;

/**
 *
 * @author Lukasz Rudnicki
 */
public class PaletteSupport
{
    private static WorkflowElement selectedElement = null;
    public static final DataFlavor WORKFLOW_ELEMENT_FLAVOR = new DataFlavor( WorkflowElement.class, "WorkflowElement" );
    private static Logger log = Logger.getLogger( PaletteSupport.class.getName() );
    
    public static PaletteController createPalette()
    {
        AbstractNode paletteRoot = new AbstractNode( new CategoryChildren() );
        paletteRoot.setName( "Palette Root" );
       
        final PaletteController palette = PaletteFactory.createPalette( paletteRoot, new MyActions(), null, new MyDnDHandler() );
        palette.addPropertyChangeListener( new PropertyChangeListener() 
        {
            public void propertyChange( PropertyChangeEvent evt )
            {
                ImportWorkflowElementWizardAction f = new ImportWorkflowElementWizardAction();
                f.performAction();
                if( PaletteController.PROP_SELECTED_ITEM.equals( evt.getPropertyName() ) ) 
                {
                    Lookup selItem = palette.getSelectedItem();
                    if( null != selItem )
                    {
                        WorkflowElement wfe = ( WorkflowElement )selItem.lookup( WorkflowElement.class );
                        PaletteSupport.setSelectedElement( wfe );
                    }
                }
            }
        } );
        return palette;
    }
    
    public static WorkflowElement getSelectedElement()
    {
        return selectedElement;
    }

    public static void setSelectedElement( WorkflowElement aSelectedElement )
    {
        selectedElement = aSelectedElement;
    }
    
    private static class MyActions extends PaletteActions
    {

        public Action[] getImportActions()
        {
            log.info( "1111111111" );
            return null;
        }

        public Action[] getCustomPaletteActions()
        {
            log.info( "22222222222" );
            return null;
        }

        public Action[] getCustomCategoryActions( Lookup lookup )
        {
            log.info( "3333333333333" );
            return null;
        }

        public Action[] getCustomItemActions( Lookup lookup )
        {
            log.info( "44444444444444" );
            return null;
        }

        public Action getPreferredAction( Lookup lookup )
        {
            log.info( "rerererere" );
            return null;
        }
    }

    private static class MyDnDHandler extends DragAndDropHandler
    {

        public void customize( ExTransferable exTransferable, Lookup lookup )
        {
            final WorkflowElement node = lookup.lookup( WorkflowElement.class );
            
            //final Image image = ( Image ) node.getIcon( BeanInfo.ICON_COLOR_16x16 );
            exTransferable.put( new ExTransferable.Single( WORKFLOW_ELEMENT_FLAVOR )
            {
                protected Object getData() throws IOException, UnsupportedFlavorException
                {
                    return node;
                }
            } );
        }
    }
}
